﻿using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using StockholmsStad.Data.Interfaces;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;

namespace StockholmsStad.Business.Test
{
    /// <summary>
    ///This is a test class for CeremonyBusinessTest and is intended
    ///to contain all CeremonyBusinessTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CeremonyBusinessTest
    {
        private IDataContext _context;
        private MockRepository _mocks = new MockRepository();
        private const User _user = null;

        #region Additional test attributes

        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}

        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void MyTestInitialize()
        {
            _mocks = new MockRepository();
        }

        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //

        #endregion

        private CeremonyBusiness GetTarget(IAgentFactory agentFactory)
        {
            var target = new CeremonyBusiness(_user);
            target.AgentFactory = agentFactory;
            _context = _mocks.DynamicMock<IDataContext>();
            IContextFactory contextFactory = new MockContextFactory(_context);
            //target.ContextFactory = contextFactory;
            return target;
        }

        /// <summary>
        ///A test for GetCertificateLanguages
        ///</summary>
        [TestMethod()]
        public void GetCertificateLanguagesTest()
        {
            IAgentFactory agentFactory = _mocks.StrictMock<IAgentFactory>();
            var languageAgent = _mocks.StrictMock<ILanguageAgent>();


            CeremonyBusiness target = GetTarget(agentFactory);
            List<ValueIdPair> expected = new List<ValueIdPair>()
                                             {
                                                 new ValueIdPair() {Id = 1, Name = "Svenska"},
                                                 new ValueIdPair() {Id = 2, Name = "Engelska"}
                                             };
            Expect.Call(agentFactory.GetLanguageAgent()).Return(languageAgent);
            Expect.Call(languageAgent.GetCertificateLanguages(_context)).Return(expected);
            IList<ValueIdPair> actual;
            _mocks.ReplayAll();
            actual = target.GetCertificateLanguages();
            Assert.AreEqual(expected, actual);
            _mocks.VerifyAll();
        }

        /// <summary>
        ///A test for GetCeremonyLanguages
        ///</summary>
        [TestMethod()]
        public void GetCeremonyLanguagesTest()
        {
            IAgentFactory agentFactory = _mocks.StrictMock<IAgentFactory>();
            ICeremonyAgent ceremonyAgent = _mocks.StrictMock<ICeremonyAgent>();
            var languageAgent = _mocks.StrictMock<ILanguageAgent>();

            CeremonyBusiness target = GetTarget(agentFactory);
            List<ValueIdPair> expected = new List<ValueIdPair>()
                                             {
                                                 new ValueIdPair() {Id = 1, Name = "Svenska"},
                                                 new ValueIdPair() {Id = 2, Name = "Engelska"}
                                             };
            Expect.Call(agentFactory.GetCeremonyAgent()).Return(ceremonyAgent);
            Expect.Call(languageAgent.GetCeremonyLanguages(_context)).Return(expected);
            IList<ValueIdPair> actual;
            _mocks.ReplayAll();
            actual = target.GetCeremonyLanguages();
            Assert.AreEqual(expected, actual);
            _mocks.VerifyAll();
        }

        [TestMethod]
        public void GetDefaultLanguage()
        {
            var agentFactory = _mocks.StrictMock<IAgentFactory>();
            var ceremonyAgent = _mocks.StrictMock<ICeremonyAgent>();
            var languageAgent = _mocks.StrictMock<ILanguageAgent>();
            ValueIdPair expected = new ValueIdPair() {Name = "Svenska", Id = 1};

            CeremonyBusiness target = GetTarget(agentFactory);
            Expect.Call(agentFactory.GetCeremonyAgent()).Return(ceremonyAgent);
            Expect.Call(languageAgent.GetDefaultLanguage(_context)).Return(expected);

            _mocks.ReplayAll();
            var actual = target.GetDefaultLangauage();

            Assert.AreEqual(expected, actual, "Det förväntade språket stämmer inte överens med det aktuella språket");
            _mocks.VerifyAll();
        }

        /// <summary>
        ///A test for GetCeremonySpecialNeeds
        ///</summary>
        [TestMethod()]
        public void GetCeremonySpecialNeedsTest()
        {
            IAgentFactory agentFactory = _mocks.StrictMock<IAgentFactory>();
            ICeremonyAgent ceremonyAgent = _mocks.StrictMock<ICeremonyAgent>();

            CeremonyBusiness target = GetTarget(agentFactory);
            List<ValueIdPair> expected = new List<ValueIdPair>()
                                             {
                                                 new ValueIdPair() {Id = 1, Name = "Rullstol"},
                                             };
            Expect.Call(agentFactory.GetCeremonyAgent()).Return(ceremonyAgent);
            Expect.Call(ceremonyAgent.GetSpecialNeeds(_context)).Return(expected);
            IList<ValueIdPair> actual;
            _mocks.ReplayAll();
            actual = target.GetCeremonySpecialNeeds();
            Assert.AreEqual(expected, actual);
            _mocks.VerifyAll();
        }

        /// <summary>
        ///A test for GetCeremonyTypes
        ///</summary>
        [TestMethod()]
        public void GetCeremonyTypesTest()
        {
            IAgentFactory agentFactory = _mocks.StrictMock<IAgentFactory>();


            CeremonyBusiness target = GetTarget(agentFactory);
            IList<ValueIdPair> actual;
            _mocks.ReplayAll();
            actual = target.GetCeremonyTypes();
            Assert.AreEqual(2, actual.Count);
            Assert.AreEqual(CeremonyType.Ordinary.ToString(), actual[0].Name);
            _mocks.VerifyAll();
        }
    }
}